package biz.bbtec.sbm.store.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.store.bean.CategoryBean;
import biz.bbtec.sbm.store.bean.ItemBean;
import biz.bbtec.sbm.store.factory.ItemFactory;
import biz.bbtec.sbm.store.service.ItemService;
import biz.bbtec.sbm.user.bean.UserBean;

public class ItemAction extends ActionSupport {

	private List<ItemBean> itemlist = new ArrayList<ItemBean>();
	private ItemBean item;
	private Map session;
	private int second;
	private Map<Integer, List<CategoryBean>> catmap = new HashMap<Integer, List<CategoryBean>>();
	private List<CategoryBean> first = new ArrayList<CategoryBean>();

	private static final String ADDCAT = "addcat";

	public Map<Integer, List<CategoryBean>> getCatmap() {
		return catmap;
	}

	public void setCatmap(Map<Integer, List<CategoryBean>> catmap) {
		this.catmap = catmap;
	}

	public List<CategoryBean> getFirst() {
		return first;
	}

	public void setFirst(List<CategoryBean> first) {
		this.first = first;
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public List<ItemBean> getItemlist() {
		return itemlist;
	}

	public void setItemlist(List<ItemBean> itemlist) {
		this.itemlist = itemlist;
	}

	public ItemBean getItem() {
		return item;
	}

	public void setItem(ItemBean item) {
		this.item = item;
	}

	public String ShowAllItem() {
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		ItemService itemservice = ItemFactory.getInstance().creattemService();
		itemlist = itemservice.showallitem(user);
		if (itemlist.size() == 0) {
			this.addActionError("您的公司目前没有任何物料记录！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String AddItem() {
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		ItemService itemservice = ItemFactory.getInstance().creattemService();
		if (!(itemservice.additem(item, user, getSecond()))) {
			this.addActionError("添加失败！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String ShowOneItem() {
		ItemService itemservice = ItemFactory.getInstance().creattemService();
		item = itemservice.showoneitem(item.getItemid());
		if (item == null) {
			this.addActionError("数据读取失败！");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		first = itemservice.showfirst(user);
		if (first.size() == 0) {
			this.addActionError("请先添加类别！");
			return ADDCAT;
		}
		for (int i = 0; i < first.size(); i++) {
			List<CategoryBean> second = new ArrayList<CategoryBean>();
			second = itemservice.showsecond(user, first.get(i).getCatid());
			if (second.size() != 0) {
				catmap.put(first.get(i).getCatid(), second);
			}
		}
		if (catmap.size() == 0) {
			this.addActionError("请先添加类别！");
			return ADDCAT;
		}
		return SUCCESS;
	}

	public String ModifyItem() {
		ItemService itemservice = ItemFactory.getInstance().creattemService();
		item.setCatid(getSecond());
		if (!(itemservice.modifyitem(item))) {
			this.addActionError("添加失败！");
			return INPUT;
		}
		return SUCCESS;

	}
}
